/*
 * ========================================================
 * Copyright(c) 2014 杭州偶尔科技-版权所有
 * ========================================================
 * 本软件由杭州偶尔科技所有, 未经书面许可, 任何单位和个人不得以
 * 任何形式复制代码的部分或全部, 并以任何形式传播。
 * 公司网址
 * 
 * 			http://www.kkkd.com/
 * 
 * ========================================================
 */
package com.simon.mengkou.ui.activity;

import android.widget.ListView;

import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.ouertech.android.agm.lib.base.future.core.AgnettyFuture;
import com.ouertech.android.agm.lib.base.future.core.AgnettyResult;
import com.ouertech.android.agm.lib.base.utils.UtilList;
import com.ouertech.android.agm.lib.ui.base.defaults.activity.BaseTopActivity;
import com.simon.mengkou.R;
import com.simon.mengkou.data.bean.base.Coupon;
import com.simon.mengkou.data.enums.EDirection;
import com.simon.mengkou.future.base.OuerFutureListener;
import com.simon.mengkou.system.constant.CstOuer;
import com.simon.mengkou.system.global.OuerApplication;
import com.simon.mengkou.ui.adapter.CouponAdapter;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * @author : Zhenshui.Xia
 * @since : 2016/3/25.
 * desc    : 优惠券界面
 */
public class CouponActivity extends BaseTopActivity
        implements PullToRefreshBase.OnRefreshListener2<ListView>{
    @Bind(R.id.common_pulltorefresh_id_list)
    PullToRefreshListView mPtrView;

    private CouponAdapter mAdapter;
    private String mMaxId = CstOuer.PAGE.DEFAULT_MAXID;

    @Override
    protected void initTop() {
        setTitle(R.string.coupon_title);
        setNavigation(R.drawable.common_ic_left_nav);
    }

    @Override
    protected void initLayout() {
        setContentView(R.layout.layout_common_pulltorefresh_list);
    }

    @Override
    protected void initViews() {
        ButterKnife.bind(this);

        mPtrView.setOnRefreshListener(this);
        mPtrView.setMode(PullToRefreshBase.Mode.DISABLED);
        mPtrView.setTag(false);

        mAdapter = new CouponAdapter(this);
        mPtrView.setAdapter(mAdapter);

        //首次失败重试
        setOnRetryListener(new OnRetryListener() {
            @Override
            public void onRetry() {
                getData(CstOuer.PAGE.DEFAULT_MAXID, EDirection.REFRESH);
            }
        });

        //获取数据
//        getData(CstOuer.PAGE.DEFAULT_MAXID, EDirection.REFRESH);

        setEmptyText(R.string.common_empty_data);
    }


    @Override
    public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
        //下拉刷新
        getData(CstOuer.PAGE.DEFAULT_MAXID, EDirection.REFRESH);
    }

    @Override
    public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
        //上拉加载更多
        getData(mMaxId, EDirection.LOADMORE);
    }

    /**
     * 获取通知消息
     * @param maxId
     * @param direction
     */
    private void getData(final String maxId, final int direction) {
        AgnettyFuture future = OuerApplication.mOuerFuture.userCoupons(
                maxId, CstOuer.PAGE.DEFAULT_MINID, CstOuer.PAGE.DEFAULT_SIZE,
                new OuerFutureListener(this){

                    public void onStart(AgnettyResult result) {
                        boolean flag = (Boolean)mPtrView.getTag();
                        if(!flag) {
                            setLoading(true);
                        }
                    }

                    @Override
                    public void onComplete(AgnettyResult result) {
                        super.onComplete(result);
                        boolean flag = (Boolean)mPtrView.getTag();
                        if(!flag) {
                            mPtrView.setTag(true);
                            setLoading(false);
                        } else {
                            mPtrView.onRefreshComplete();
                        }

                        List<Coupon> datas = (List<Coupon>) result.getAttach();

                        if(direction == EDirection.REFRESH) {
                            if(UtilList.isEmpty(datas)) {
                                mPtrView.setMode(PullToRefreshBase.Mode.DISABLED);
                                setEmptyText(R.string.common_empty_data);
                                mAdapter.clearAll();
                            } else {
                                int count = UtilList.getCount(datas);
                                //设置最新的max id
                                if(count != 0) {
                                    mMaxId = datas.get(count - 1).getId();
                                }

                                if(count < CstOuer.PAGE.DEFAULT_SIZE) {
                                    mPtrView.setMode(PullToRefreshBase.Mode.PULL_FROM_START);
                                } else {
                                    mPtrView.setMode(PullToRefreshBase.Mode.BOTH);
                                }

                                mAdapter.setList(datas);
                            }
                        } else {
                            int count = UtilList.getCount(datas);
                            //设置最新的max id
                            if(count != 0) {
                                mMaxId = datas.get(count - 1).getId();
                            }

                            //数据已读取完
                            if(count < CstOuer.PAGE.DEFAULT_SIZE) {
                                mPtrView.setMode(PullToRefreshBase.Mode.PULL_FROM_START);
                            }

                            mAdapter.addList(datas);
                        }
                    }

                    public void onNetUnavaiable(AgnettyResult result) {
                        super.onNetUnavaiable(result);
                        boolean flag = (Boolean)mPtrView.getTag();
                        if(!flag) {
                            setRetry(true);
                        } else {
                            mPtrView.onRefreshComplete();
                        }
                    }

                    @Override
                    public void onException(AgnettyResult result) {
                        super.onException(result);
                        boolean flag = (Boolean)mPtrView.getTag();
                        if(!flag) {
                            setRetry(true);
                        } else {
                            mPtrView.onRefreshComplete();
                        }
                    }
                });

        attachDestroyFutures(future);
    }
}
